from xiaoyiwy.xiaoyi.config import *
import re

db = mysql()

# 计算积分
def balance_post(openid):
    conn = db.connection()
    cursor = conn.cursor()
    open_id = openid['openid']
    if openid['openid']:
        sql = "select count(id) from drug_box where open_id = '%s'" % open_id
        sql2 = "select count(id) from drug_barcode where open_id = '%s'" % open_id
        cursor.execute(sql)
        conn.commit()
        balance1 = cursor.fetchall()[0][0]
        cursor.execute(sql2)
        conn.commit()
        balance2 = cursor.fetchall()[0][0]
        data_json = {
            "code": 1,
            "error": "",
            "balance": balance1+balance2,
            "level": "L1用户"
        }
    else:
        data_json = {
            "code": 0,
            "error": "openid为空",
            "balance": "",
            "level": ""
        }
    mysql_close(cursor,conn)
    return data_json

# 历史扫描
def history_qrcode(openid):
    conn = db.connection()
    cursor = conn.cursor()
    open_id = openid['openid']
    if open_id:
        sql = "select drug_id,drug_name,update_time from drug_barcode where open_id = '%s'" % open_id
        cursor.execute(sql)
        sql_data = sql_index_dict(cursor)
        mysql_close(cursor, conn)
        data = {
            "code": 1,
            "error": "",
            "data" : sql_data
        }
    else:
        data = {
            "code": 0,
            "error": "openid为空",
            "data": ""
        }
    return data